﻿using System;
using System.Web;
using System.IO;
using Heart;
using Common;
namespace FCKEditorSimpleNet.Web.MyImageUploadHandler
{
    public class Default : FredCK.FCKeditorV2.ImageUpload.Base
    {
        /// <summary>
        /// 允许的上传图片大小(KB)
        /// </summary>
        private const int FILE_MAX = 2024;

        public override void Save()
        {
            if (this.PostFile != null)
            {
                if (this.PostFile.ContentLength > 0)
                {
                    if (this.PostFile.ContentLength <= FILE_MAX * 1024)
                    {
                        if (this.PostFile.ContentType == "image/pjpeg" || this.PostFile.ContentType == "image/jpeg" || this.PostFile.ContentType == "image/gif" || this.PostFile.ContentType == "image/bmp" || this.PostFile.ContentType == "image/png" || this.PostFile.ContentType == "image/x-png" || this.PostFile.ContentType == "image/jpg")
                        {
                            
                            //保存
                            string fileName = WebBase.GetFileName() + ".jpg";
                            string path = Server.MapPath("~/upload/product/desc/");
                            DirectoryInfo dif =new DirectoryInfo(path);
                            if (!dif.Exists)
                                dif.Create();
                            this.PostFile.SaveAs(path + fileName);

                            //客户端响应
                            FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(true, "/upload/product/desc/" + fileName, "图片上传成功！");
                        }
                        else
                        {
                            FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "图片格式不正确！目前支持JPG、GIF、BMP与PNG格式" + this.PostFile.ContentType   );
                        }
                    }
                    else
                    {
                        FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "图处大小不能超过" + FILE_MAX.ToString() + "KB！");
                    }
                }
                else
                {
                    FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "未获取图片数据！");
                }
            }
            else
            {
                FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "未获取图片对象！");
            }
        }

    }//end class
}
